﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;
using System.IO;

namespace OlapProject
{
    class WebLoader
    {
        private HtmlElementFlag flag = HtmlElementFlag.Empty;
        System.Net.WebClient client = new System.Net.WebClient();

        public bool downloadHtmlDocument(string url,  HtmlAgilityPack.HtmlDocument document)
        {
            try
            {
                var html = client.DownloadString(url);
                document.LoadHtml(html);
            }
            catch(Exception  ex)
            {                
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }

        public void disableXHTMLTags()
        {
            //remove <option> becouse it is XHTML tag and causes errors
            // http://stackoverflow.com/questions/4755486/xhtml-parsing-with-htmlagilitypack
            flag = HtmlNode.ElementsFlags["option"];
            HtmlNode.ElementsFlags.Remove("option");   
         
        }

        public void enableXHTMLTags()
        {      
            HtmlNode.ElementsFlags.Add("option", flag);
        }
    }
    
}
